import axios from 'axios'
import { ElMessage } from 'element-plus'

const request = axios.create({
  baseURL: 'http://6eab9aa1.r6.cpolar.top',
  timeout: 8000
})


request.interceptors.request.use(config => {
  const token = localStorage.getItem('token')
  if (token) config.headers.Authorization = `Bearer ${token}`
  // console.log('请求11111111配置:', config);
  return config
})

request.interceptors.response.use(
  res => res.data,
  err => {
    if (err.response?.status === 401) {
      localStorage.removeItem('token')
      location.href = '/login'
      ElMessage.error('登录已失效')
    } else {
      ElMessage.error(err.response?.data?.msg || '网络异常')
    }
    return Promise.reject(err)
  }
)

export default request